<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/11
 * Time: 14:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;

use app\common\lib\Status;

class Menu extends Base
{
    /**
     * 查询所有菜单
     * @param string $field
     * @param int $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByListInfo(string $field='*',int $userId = 0): array
    {
        $where = [];
        if($userId != 0){
            $rule = (new Rule())->getBaseById($userId);
            if($rule){
                $where[] = [
                    ['id','in',$rule['rule']]
                ];
            }
        }
        return $this->field($field)
            ->where('status', Status::mysql_normal)
            ->where($where)
            ->order('sort asc,id asc')
            ->select()
            ->toArray();
    }
    public function getMenuByHomeId(int $pid = 0){
        return $this->where('pid',$pid)
            ->value('id');
    }
}
